/*
 *	Copyright © 2013 Changsha Shishuo Network Technology Co., Ltd. All rights reserved.
 *	长沙市师说网络科技有限公司 版权所有
 *	http://www.shishuo.com
 */

package com.shishuo.cms.action;

import java.text.ParseException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shishuo.cms.Directory;
import com.shishuo.cms.constant.FolderConstant;
import com.shishuo.cms.entity.Folder;
import com.shishuo.cms.entity.Guestbook;
import com.shishuo.cms.entity.vo.ArticleVo;
import com.shishuo.cms.entity.vo.FolderVo;
import com.shishuo.cms.entity.vo.JsonVo;
import com.shishuo.cms.exception.UploadException;
import com.shishuo.cms.service.GuestbookService;

/**
 * @author Herbert
 * 
 */
@Controller
@RequestMapping("/article")
public class ArticleAction extends BaseAction {
	
	@Autowired
	private GuestbookService guestbookService;
	@RequestMapping(value = "/{articleId}.htm", method = RequestMethod.GET)
	public String folder(@PathVariable long articleId,
			@RequestParam(value = "p", defaultValue = "1") long p,
			ModelMap modelMap) {
		try {
			ArticleVo article = fileService.getArticleById(articleId);
			Folder folder = folderService.getFolderById(article.getFolderId());
			modelMap.addAttribute("p", p);
			modelMap.addAttribute("folder", folder);
			modelMap.addAttribute("article", article);
			modelMap.addAttribute("g_folderId", folder.getFolderId());
			long artFolder=0;
			long dirId=article.getFolderId();
			if (Directory.NEWS.getType()==article.getFolderId()) {
				List<FolderVo> folderVos=folderService.getFolderListByFatherId(article.getFolderId(), FolderConstant.status.display);
				if (folderVos.size()>0) {
					artFolder=folderVos.get(0).getFolderId();
				}
			}else if(Directory.NEWS.getType()==folder.getFatherId()){
				artFolder=folder.getFolderId();
				dirId=folder.getFatherId();
			}
			List<FolderVo> pathList = folderService
					.getFolderPathListByFolderId(artFolder);
			modelMap.put("pathList", pathList);
			modelMap.put("article", article);
			modelMap.put("newFolder", Directory.NEWS.getType());
			modelMap.put("folder", folder);
			modelMap.put("g_folderId", dirId);
			modelMap.put("news_actId", artFolder);
			modelMap.put("p", p);
			return "/template/blog/newsDetail";
		} catch (Exception e) {
			logger.fatal(e.getMessage());
			return themeService.get404();
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/add.json", method = RequestMethod.POST)
	public JsonVo<Guestbook> addJson(
			@RequestParam("name") String name,
			@RequestParam("email") String email,
			@RequestParam(value = "title", required = false) String title,
			@RequestParam("content") String content,
			HttpServletRequest request, ModelMap modelMap)
			throws UploadException, ParseException {
		JsonVo<Guestbook> json = new JsonVo<Guestbook>();
		Guestbook guestbook = guestbookService.addGuestbook(name,email, title,content);
		json.setT(guestbook);
		json.setResult(true);
		return json;
	}
}
